import 'package:test/test.dart';

void main() {
  group('test1', () {
    test('value should start at 0', () {
      late var value = getValue(true);
      print(value);
    });

    test('test2', () {
      var value = getValue(false);
      if (value == null) {
        throw Exception("未初始化");
      }
    });

    test("test3", () {
      String name = getValue(false)!;
      print(name);
    });

    test("test4", () {
      var counter = Counter(1);
      counter.increase();
      print(counter);

      counter.count = 5;
      print(counter);

      const array = [1, 2, 4];
      print(array[0]);
    });

    test("test5", () {
      const array = [1, 2, 4];
      print(array[0]);

      final array2 = ["f", "4f", "fd"];
      print(array2[2]);
    });
    test("test6", () {
      var numValue = 1.toString();
      print(numValue);

      var num = double.parse("3.2455");
      print(num);

      var strValue = "hello";

      var value =
          "22$numValue,numValue:${strValue}numValueLength:${strValue.length}";
      print(value);

      var s1 = "ff"
          "222"
          "@23";
      print(s1);

      var s2 = """
      
select * from tb_user
where id = "111"
order by created_date desc
      
      """;
      print(s2);
    });

    test("test7", () {
      // List<String> list1 = ["l423", "fdf"];
      var list1 = ["l423", "fdf"];
      list1.forEach((value) {
        print(value);
      });

      Set<String> set1 = {"fsadf", "3333"};
      set1.forEach((value) {
        print(value);
      });
      for (var value in set1) {
        print(value);
      }

      Set<String> set2 = {};
      print("确定类型搭配{}默认类型是set,set2:${set2.runtimeType}");

      var set3 = {};
      print("var搭配{}默认类型是map,set3:${set3.runtimeType}");

      var map1 = {"key1": "2", "key3": "ffsf"};
      print("\nmap1 value:");
      map1.forEach((key, value) {
        print("key:$key,vlaue:$value");
      });

      var map2 = {};
      map2[2] = "33";
      map2[11] = "fda";
      print("\nmap2 value:");
      map2.forEach((key, value) {
        print("key:$key,vlaue:$value");
      });

      var map3 = Map();
      map3[Counter(1)] = "fff";
      map3[Counter(2)] = "ggg";
      map3[Counter(1)] = "fff1";
      print("\nmap3 value:");
      map3.forEach((key, value) {
        print("key:$key,vlaue:$value");
      });
    });

    test("test8", () {
      var value1 = buildString("lisa", "hello");
      print(value1);

      var value2 = buildString("张伟", "youyou", "你干嘛");
      print(value2);

      var value3 =
          buildString2(value: "张伟", suffix: "好好好", aliases: ["你", "是呆瓜", "吗？"]);
      print(value3);
    });

    test("test9", (){

      var i = 1;
      print("i is Counter:${i is Counter}");
      print("i is int:${i is int}");
      print("i is not String:${i is! String}");
      print("i is Object:${i is Object}");

      Counter? get(bool init,[int value = 1]) {
        return init ? Counter(value) : null;
      }

      Object? getObject() {
        return null;
      }


      var value1 = get(true);
      var value2 = get(true, 2);
      var value3 = getObject();
      // var value2 = get(false);
      // 当value2值为空时，才会将value1赋值为value2
      value2 ??= value1;

      // 如果name不为空，则返回name的值，如果name为空，返回Guest
      String playerName(String? name) => name ?? 'Guest';

      print((value1 as Counter).count);
      print((value2 as Counter)?.count);

      print("1111");
      print((value3 as Counter?)?.count);
      print(playerName(null));
      print(playerName("ffff"));


      /**
       *
       * 级联运算符 (..) 可以实现对同一个对像进行一系列的操作
       *
       *
       * querySelector('#confirm') // 获取对象。
          ..text = 'Confirm' // 调用成员变量。
          ..classes.add('important')
          ..onClick.listen((e) => window.alert('Confirmed!'));

          上面的代码等价于：
          var button = querySelector('#confirm');
          button.text = 'Confirm';
          button.classes.add('important');
          button.onClick.listen((e) => window.alert('Confirmed!'));
       *
       *
       * 级联运算符可以嵌套，例如：
       *
       * final addressBook = (AddressBookBuilder()
          ..name = 'jenny'
          ..email = 'jenny@example.com'
          ..phone = (PhoneNumberBuilder()
          ..number = '415-555-0100'
          ..label = 'home')
          .build())
          .build();
       *
       */









    });
  });
}

/*
 * 可选参数
 */
String buildString(String value, String prefix, [String suffix = ""]) {
  return "$prefix-$value-$suffix";
}

/*
 * 命名参数
 */
String buildString2(
    {required String value,
    bool? flag,
    String prefix = "hello",
    String suffix = "",
    List<String> aliases = const []}) {
  String all = "";

  /*
  函数中可以套用函数
   */
  void handle(String value) {
    all += "$value,";
  }

  // 函数作为变量传入
  aliases.forEach(handle);
  return "$prefix-$value-$all-$suffix";
}

class Counter {
  int count;

  Counter(this.count);

  void increase() {
    count++;
  }

  @override
  int get hashCode {
    return count;
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    if (other is! Counter) {
      return false;
    }
    return count == other.count;
  }

  @override
  String toString() {
    return "Count value:$count";
  }
}

String? getValue(bool init) {
  print("1111");
  if (init) {
    return "aaa";
  } else {
    return null;
  }
}
